# Makefile for compiling C files with different standards
# C89, C99, and C11 standard examples

# Compiler
CC = gcc

# Compiler flags for each standard
C89_FLAGS = -std=c89 -Wall -Wextra -pedantic
C89_DEBUG_FLAGS = -std=c89 -Wall -Wextra -pedantic -g
C99_FLAGS = -std=c99 -Wall -Wextra -pedantic
C99_DEBUG_FLAGS = -std=c99 -Wall -Wextra -pedantic -g
C11_FLAGS = -std=c11 -Wall -Wextra -pedantic
C11_DEBUG_FLAGS = -std=c11 -Wall -Wextra -pedantic -g

# Create Release directory
.PHONY: init
init:
	mkdir -p Release

# Create Debug directory
.PHONY: init-debug
init-debug:
	mkdir -p Debug

# Executables
C89_EXEC = ./Release/c89
C99_EXEC = ./Release/c99
C11_EXEC = ./Release/c11
#
C89_DEBUG_EXEC = ./Debug/c89
C99_DEBUG_EXEC = ./Debug/c99
C11_DEBUG_EXEC = ./Debug/c11

# Source files
C89_SRC = c89.c
C99_SRC = c99.c
C11_SRC = c11.c

# target
.PHONY: all
all: init $(C89_EXEC) $(C99_EXEC) $(C11_EXEC)

# Debug target
.PHONY: debug
debug: init-debug $(C89_DEBUG_EXEC) $(C99_DEBUG_EXEC) $(C11_DEBUG_EXEC)

# Compile C89 standard executable
$(C89_EXEC): $(C89_SRC)
	$(CC) $(C89_FLAGS) $< -o $@

# Compile C99 standard executable
$(C99_EXEC): $(C99_SRC)
	$(CC) $(C99_FLAGS) $< -o $@

# Compile C11 standard executable
$(C11_EXEC): $(C11_SRC)
	$(CC) $(C11_FLAGS) $< -o $@

# Compile C89 standard debug executable
$(C89_DEBUG_EXEC): $(C89_SRC)
	$(CC) $(C89_DEBUG_FLAGS) $< -o $@

# Compile C99 standard debug executable
$(C99_DEBUG_EXEC): $(C99_SRC)
	$(CC) $(C99_DEBUG_FLAGS) $< -o $@

# Compile C11 standard debug executable
$(C11_DEBUG_EXEC): $(C11_SRC)
	$(CC) $(C11_DEBUG_FLAGS) $< -o $@

# Clean compiled executables
.PHONY: clean
clean:
	rm -f $(C89_EXEC) $(C99_EXEC) $(C11_EXEC)
	rm -f $(C89_DEBUG_EXEC) $(C99_DEBUG_EXEC) $(C11_DEBUG_EXEC)

# Run all programs
.PHONY: run
run: all
	./$(C89_EXEC)
	./$(C99_EXEC)
	./$(C11_EXEC)

# Run all debug programs
.PHONY: run-debug
run-debug: debug
	./$(C89_DEBUG_EXEC)
	./$(C99_DEBUG_EXEC)
	./$(C11_DEBUG_EXEC)

# Run C89 program
.PHONY: run-c89
run-c89: init $(C89_EXEC)
	./$(C89_EXEC)

# Run C99 program
.PHONY: run-c99
run-c99: init $(C99_EXEC)
	./$(C99_EXEC)

# Run C11 program
.PHONY: run-c11
run-c11: init $(C11_EXEC)
	./$(C11_EXEC)

# Run C89 debug program
.PHONY: run-debug-c89
run-debug-c89: init-debug $(C89_DEBUG_EXEC)
	./$(C89_DEBUG_EXEC)

# Run C99 debug program
.PHONY: run-debug-c99
run-debug-c99: init-debug $(C99_DEBUG_EXEC)
	./$(C99_DEBUG_EXEC)

# Run C11 debug program
.PHONY: run-debug-c11
run-debug-c11: init-debug $(C11_DEBUG_EXEC)
	./$(C11_DEBUG_EXEC)
